<?php
class CatalogCustomFields extends CActiveRecord
{
	public static function model($className=__CLASS__)
	{
            return parent::model($className);
	}

	public function tableName()
	{
            return 'CatalogCustomFields';
	}

	public function rules()
	{

            return array(
                    array('label, c_c_id, l_id', 'required'),
                    array('position, c_c_id, l_id, parent_id', 'numerical', 'integerOnly'=>true),
                    array('label', 'length', 'max'=>255),
                    array('id, position, label, c_c_id, l_id, parent_id', 'safe', 'on'=>'search'),
		);
	}
        
	public function relations()
	{
            return array(
                    'catalogCustomFieldValues' => array(self::HAS_MANY, 'CatalogCustomFieldValue', 'c_c_f_id'),
                    'l' => array(self::BELONGS_TO, 'Language', 'l_id'),
                    'cC' => array(self::BELONGS_TO, 'CatalogCategory', 'c_c_id'),
		);
	}

	public function attributeLabels()
	{
            return array(
                    'id' => Yii::t('system','ID'),
                    'position' => Yii::t('system','Position'),
                    'label' => Yii::t('system','Label'),
                    'c_c_id' => Yii::t('system','C C'),
                    'l_id' => Yii::t('system','L'),
                    'parent_id' => Yii::t('system','Parent'),
            );
	}
        
        
    //Результат переведенные и не переведенные категории
    public static function getFeieldTranslations($id, $c_c_id, $position){
        //Выборка
        $command=Yii::app()->db->createCommand()
            ->select('c.id as field_id, c.label, l.name, l.id as language_id')
            ->from('CatalogCustomFields c')
            ->join('Language l', 'c.l_id = l.id')
            ->where('c.parent_id = :id',array(':id'=>$id))
            ->queryAll();
        
        //Количество языков. От минусовываем дефолтовый язык
        $count_language = Language::countLang()-1;
        //Имеющиеся переводы
        $language_for_item = Array();
        
        //То на что есть перевод
        if (count($command)>0){
            foreach ($command as $key => $value) {
                $language_for_item[] = $value['language_id'];
                $view .= CHtml::link($value['name'].' - '.$value['label'],array('/catalog/CatalogCategory/update','id'=>$value['page_id'])).'<br>';
            }
        }
        //Остальое без перевода
        if ($count_language > count($command)){
            foreach (Language::allExcept($language_for_item) as $value) {
                $view .= CHtml::link($value['name'].' - Нет перевода',array('/catalog/CatalogCategory/create','language_id'=>$value['id'],'lang_parent_id'=>$id,'parent_id'=>$parent_id),array('class'=>'warning')).'<br>';
            }
        }
        return $view;
    }
    
        public static function getFieldsOfCategoryDefaultLanguage($category_id){
            $dataReader=Yii::app()->db->createCommand()
                ->select('label, id')
                ->from('CatalogCustomFields')
                ->where('l_id=:language and c_c_id=:category_id',array(':category_id'=>$category_id,':language' => Language::getIdDefalutLanguage()))
                ->queryAll();
            return $dataReader;
        }

        public static function getFieldsAndValueOfCategoryCurrentLanguage($category_id){
            $dataReader=Yii::app()->db->createCommand()
                ->select('label, id')
                ->from('CatalogCustomFields')
                ->where('l_id=:language and c_c_id=:category_id',
                        array(
                            ':category_id'=>$category_id,
                            ':language' => Language::getId()
                        ))
                ->queryAll();
            return $dataReader;
        }
        
        public static function getListFieldsOfCategoryDefaultLanguage($category_id){
            $fields_array = self::getFieldsOfCategoryDefaultLanguage($category_id);
            $view = '<ul>';
            if (count($fields_array)>0){
                foreach ($fields_array as $field) {
                    $view .= '<li>'.$field['label'].'</li>';
                }
            }
            $view .= '<li>'.CHtml::link('Добавить',array('/catalog/catalogCustomFields/create','c_c_id'=>$category_id)).'</li>';
            $view .= '</ul>';            
            return $view;
        }

        


        public function dropDownItemsAll(){
            $items= Array();
            $model = self::model()->findAll();
            foreach ($model as $value) {
                $items[$value->id] = $value->id;
            }
            return $items;
        }
        
	public function search()
	{	
            $criteria=new CDbCriteria;
            $criteria->compare('id',$this->id);
            $criteria->compare('position',$this->position);
            $criteria->compare('label',$this->label,true);
            $criteria->compare('c_c_id',$this->c_c_id);
            $criteria->compare('l_id',$this->l_id);
            $criteria->compare('parent_id',$this->parent_id);
            return new CActiveDataProvider($this, array(
                    'criteria'=>$criteria,
            ));
	}
}


//    public static function allLang(){
//        $dataReader=Yii::app()->db->createCommand()
//        ->select('l,name')
//        ->from(array('Language'))
//        ->queryAll();
//        return $dataReader;
//    }
//    
//    public static function countLang(){
//        $sql = "SELECT COUNT(*) as count FROM `Language`";
//        $dataReader=Yii::app()->db->createCommand($sql)->queryRow();
//        return $dataReader['count'];
//    }
//    
//    public static function allExcept($except_id){
//        $dataReader=Yii::app()->db->createCommand()
//        ->select('*')
//        ->from(array('Language'))
//        ->where(array('and', '`primary` <> :primary', array('not in','id',$except_id)),array(':primary'=>'1'))
//        ->queryAll();
//        return $dataReader;
//    }